Program chgs_onloan_sttl_rets
      character*6 ticker(5000)
      character*8 cusp(5000)
      integer(kind=8):: nshrs(5000),nvalue(5000),jvalue(5000),ivol(5000),numshrs(5000)
      dimension jast(5000),jdate(5000),itickt(5000),age(5000),favg(5000),fmax(5000),fmin(5000),fstd(5000),iperm(5000)
      dimension nep(5000),price(5000),ishrout(5000),vwret(5000),util(5000),retrn(5000),dfee(5000),ntype(1280)
!      open(unit=10,file='e:\FIS\episode_data.txt')
!      open(unit=11,file='e:\FIS\episode_results.txt')
!      open(unit=99,file='e:\FIS\episode_rets_chg_trddate.txt')
      open(unit=10,file='e:\FIS\episode_data_rep.txt')
      open(unit=11,file='e:\FIS\episode_results_rep.txt')
      open(unit=99,file='e:\FIS\episode_rets_chg_trddate_rep.txt')
      iend=0
      do 700 i=1,1264
700        read(11,1100)ntype(i)
1100  format(6x,i1)          
      i=0
1     i=i+1
2     read(10,1000,end=900)nep(i),jast(i),jdate(i),cusp(i),ticker(i),itickt(i),numshrs(i),age(i),favg(i),fmax(i),fmin(i),fstd(i),iperm(i),price(i),ivol(i),retrn(i),ishrout(i),vwret(i),util(i),nshrs(i)      
!      write(6,1000)nep(i),jast(i),jdate(i),cusp(i),ticker(i),itickt(i),numshrs(i),age(i),favg(i),fmax(i),fmin(i),fstd(i),iperm(i),price(i),ivol(i),retrn(i),ishrout(i),vwret(i),util(i),nshrs(i)      
1000  format(1x,i4,2(1x,i8),1x,a8,2x,a6,4x,i9,1x,i14,5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13)    
      if(nep(i).eq.nep(1)) go to 1
5     icnt=i-1
      if(icnt.lt.3) go to 500
      omax=float(numshrs(1))/(1000.*float(ishrout(1)))
      obeg=float(numshrs(1))/(1000.*float(ishrout(1)))
      oend=float(numshrs(icnt))/(1000.*float(ishrout(icnt)))
      nstl=1
      do 10 ii=2,icnt
          orat=float(numshrs(ii))/(1000.*float(ishrout(ii)))
          if(orat.lt.omax) go to 10
          omax=orat
          nstl=ii
10    continue
      numo=nstl-2
      if(jdate(nstl).lt.20170322) numo=nstl-3
      if(numo.lt.2) retb=-9.99
      if(numo.lt.2) go to 20
      tret=1.
      do 15 ii=2,numo
15    tret=tret*(1.+retrn(ii))
      retb=tret-1.
20    continue
      if(numo.eq.icnt) reta=-9.99
      if(numo.eq.icnt) go to 30
      tret=1.
      numop=numo+1
      do 25 ii=numop,icnt
          if(ii.lt.1) go to 25
      tret=tret*(1.+retrn(ii))
25    continue      
      reta=tret-1.
30    continue      
      
      aretb=-9.99
      areta=-9.99
      nsta=-9
      amax=-.99
      aend=-.99
      abeg=-.99
      if(nshrs(1).lt.0) go to 60
      amax=float(nshrs(1))/(1000.*float(ishrout(1)))
      abeg=float(nshrs(1))/(1000.*float(ishrout(1)))
      aend=float(nshrs(icnt))/(1000.*float(ishrout(icnt)))
      nsta=1
      do 40 ii=2,icnt
          arat=float(nshrs(ii))/(1000.*float(ishrout(ii)))
          if(arat.lt.amax) go to 40
          amax=arat
          nsta=ii
40    continue
      numa=nsta-2
      if(jdate(nstl).lt.20170322) numa=nsta-3
      if(numa.lt.2) aretb=-9.99
      if(numa.lt.2) go to 50
      tret=1.
      do 45 ii=2,numa
45    tret=tret*(1.+retrn(ii))
      aretb=tret-1.
50    continue
      if(numa.eq.icnt) areta=-9.99
      if(numa.eq.icnt) go to 60
      tret=1.
      numap=numa+1
      do 55 ii=numap,icnt
          if(ii.lt.1) go to 55
      tret=tret*(1.+retrn(ii))
55    continue      
      areta=tret-1.
60    continue      

      uretb=-9.99
      ureta=-9.99
      nstu=-9
      umax=-.99
      uend=-.99
      ubeg=-.99
      if(nshrs(1).lt.0) go to 90
      umax=util(1)
      ubeg=util(1)
      uend=util(icnt)
      nstu=1
      do 70 ii=2,icnt
          if(util(ii).lt.umax) go to 70
          umax=util(ii)
          nstu=ii
70    continue
      numu=nstu-2
      if(jdate(nstu).lt.20170322) numu=nstu-3
      if(numu.lt.2) uretb=-9.99
      if(numu.lt.2) go to 80
      tret=1.
      do 75 ii=2,numu
75    tret=tret*(1.+retrn(ii))
      uretb=tret-1.
80    continue
      if(numu.eq.icnt) ureta=-9.99
      if(numu.eq.icnt) go to 90
      tret=1.
      numup=numu+1
      do 85 ii=numup,icnt
          if(ii.lt.1) go to 85
      tret=tret*(1.+retrn(ii))
85    continue      
      ureta=tret-1.
90    continue      

      
      write(6,6000)nep(1),ticker(1),ntype(nep(1)),icnt,numo,obeg,omax,oend,retb,reta,numa,abeg,amax,aend,aretb,areta,numu,ubeg,umax,uend,uretb,ureta
      write(99,6000)nep(1),ticker(1),ntype(nep(1)),icnt,numo,obeg,omax,oend,retb,reta,numa,abeg,amax,aend,aretb,areta,numu,ubeg,umax,uend,uretb,ureta
6000  format(1x,i4,1x,a6,1x,i1,2(1x,i4),3(1x,f9.6),2(1x,f9.6),1x,i4,3(1x,f9.6),2(1x,f9.6),i4,3(1x,f9.5),2(1x,f9.6))      
      
    
500   nep(1)=nep(i)
      jast(1)=jast(i)
      jdate(1)=jdate(i)
      cusp(1)=cusp(i)
      ticker(1)=ticker(i)
      itickt(1)=itickt(i)
      numshrs(1)=numshrs(i)
      age(1)=age(i)
      favg(1)=favg(i)
      fmax(1)=fmax(i)
      fmin(1)=fmin(i)
      fstd(1)=fstd(i)
      iperm(1)=iperm(i)
      price(1)=price(i)
      ivol(1)=ivol(i)
      retrn(1)=retrn(i)
      ishrout(1)=ishrout(i)
      vwret(1)=vwret(i)
      util(1)=util(i)
      nshrs(1)=nshrs(i)
      i=1
      go to 1
900   continue
      iend=iend+1
      if(iend.eq.1) go to 5
      stop
end program chgs_onloan_sttl_rets